package com.demo.coroutinedemo1

import android.os.Bundle
import android.os.SystemClock
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import com.demo.coroutinedemo1.databinding.ActivityMainBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext

class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {//这是第二种方式，继承CoroutineScope但是由MainScope实现
    val TAG = "MainActivity"

    //    val mMainVM: MainVM by lazy { ViewModelProvider(this).get(MainVM::class.java) }
    val mMainVM: MainVM by viewModels()//这个必须用起来
    val mainScope= MainScope()//随着Activity的生命周期而取消协程，这是第一种方式

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        val mainBinding =
            DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        mainBinding.viewModel = mMainVM
        mainBinding.lifecycleOwner = this
//        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        mMainVM.getUserName()

        runBlocking {
            val job = async(context = Dispatchers.IO, start = CoroutineStart.UNDISPATCHED) { }
            Log.e(TAG, "thread.name:${Thread.currentThread().name}")
        }

//        launch(Dispatchers.Main) {
//            delay(10000)
//            try {
//
//            } catch (e: Exception)
//            {
//                e.printStackTrace()
//            }
//            Log.e(TAG,"延时到了")
//        }
        mainBinding.tvMainContent.setOnClickListener {
            GlobalScope.launch {
                Log.i(TAG, "拆解字符串")
                "abc".substring(10)
            }
        }

//        val launch = GlobalScope.launch(Dispatchers.Main) {
//            println("thread.name.1:${Thread.currentThread().name}")
//            val haha = withContext(Dispatchers.IO) {
//                println("thread.name.2:${Thread.currentThread().name}")
//                getHaha()
//            }
//            println("thread.name.3:${Thread.currentThread().name}")
//            println("haha:$haha")
//        }
//        launch.cancel()

        GlobalScope.launch(Dispatchers.Main) {
            println("5秒时间一到1")
            delay(5000)
            println("5秒时间一到2")
        }
        mainScope.launch {//第一种方式

        }
        launch{//第二种方式

        }
    }

    private suspend fun getHaha():String{
        SystemClock.sleep(3000)
        return "haha"
    }

    override fun onDestroy() {
        super.onDestroy()
        cancel()//这是第二种方式取消
        mainScope.cancel()//这是第一种方式取消
    }
}